package lab7;

/*
 * 
 * @author:NguyenTrongTri
 * @Created date: 5/27/2014
 */

import java.util.Random;

public class ArrayStack<T> implements StackADT<T> {

	private int arrSize;
	private T[] contents;
	private int top = -1;

	public ArrayStack() {
		contents = (T[]) new Object[100];

	}

	@Override
	public void push(T element) {
		if (top + 1 < arrSize) {
			top++;
			contents[top] = element;
		} else
			System.out.println("");
		System.out.println("PUSH " + element + " Was add to the stack\n");

	}

	@Override
	public T pop() {
		if (top == 0) {
			System.out.println("stack rong ");
			return null;

		} else {
			T result = contents[top - 1];
			contents[top - 1] = null;
			top--;
			return result;

		}
	}

	@Override
	public T peek() {
		if (top == 0) {
			System.out.println("stack rong");

			return null;
		} else {
			return contents[top - 1];
		}
	}

	@Override
	public boolean isEmpty() {
		return (top == -1);

	}

	@Override
	public int size() {

		return top;
	}

	public String toString() {
		String a = " [ ";
		boolean check = false;
		for (int i = 0; i < top; i++) {
			a += contents[i].toString() + ";";
			check = true;

		}
		if (check)
			a = a.substring(0, a.length() - 1);
		a += "]";
		return a;

	}

}
